/**
 * 
 */
package com.jolestar.yayaweibotong.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.jolestar.yayaweibotong.ServiceType;
import com.jolestar.yayaweibotong.dao.AccountDomain;
import com.jolestar.yayaweibotong.dao.DomainFactory;
import com.jolestar.yayaweibotong.dao.StatusDomain;
import com.jolestar.yayaweibotong.service.StatusService;
import com.jolestar.yayaweibotong.service.UserService;

/**
 * @author jolestar@gmail.com
 * 
 */
public abstract class AbstractStatusService<T> implements StatusService {

	private Map<String, T> instanceCache = new HashMap<String, T>();

	protected abstract T createInstance(AccountDomain account);

	protected T getOrCreateInstance(AccountDomain account) {
		T instance = this.instanceCache.get(account.getKey().getName());
		if (instance == null) {
			instance = this.createInstance(account);
			this.instanceCache.put(account.getKey().getName(), instance);
		}
		return instance;
	}

	//TODO cache
	protected boolean existsStatus(ServiceType serviceType, String originalId) {
		return UserService.ServiceHolder.getInstance().exists(
				StatusDomain.class, serviceType, originalId);
	}
	
	//TODO cache
	protected StatusDomain getStatusDomain(ServiceType serviceType, String originalId){
		return DomainFactory.load(StatusDomain.class, serviceType, originalId);
	}
}
